package dao.Impl;

import dao.BaseDao;
import dao.RainManageDao;
import entiy.RainManage;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class RainManageDaoImpl extends BaseDao implements RainManageDao {
    @Override
    public List<RainManage> getList() {
        List<RainManage> rainManagesList =new ArrayList<>();
        ResultSet rs =null;
        String sql="select * from rainquality";
        try {
            rs=executeQuery(sql);
            while (rs.next()){
                RainManage rainManage=new RainManage();
                rainManage.setId(rs.getInt("id"));
                rainManage.setDistrictName(rs.getString("districtName"));
                rainManage.setMonitorTime(rs.getString("monitorTime"));
                rainManage.setRain(rs.getString("rain"));
                rainManage.setMonitoringStation(rs.getString("monitoringStation"));
                rainManage.setMonitoringAddress(rs.getString("monitoringAddress"));

                rainManagesList.add(rainManage);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,rs);
        }
        return rainManagesList;
    }




    @Override
    public int add(RainManage rainManage) {
        int i=0;
        String sql="insert into rainquality(districtName,monitorTime,rain,monitoringStation,monitoringAddress)values(?,?,?,?,?)";
        try {
            i=executeUpdate(sql,rainManage.getDistrictName(),rainManage.getMonitorTime(),rainManage.getRain(),rainManage.getMonitoringStation(),rainManage.getMonitoringAddress());

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,null);
        }
        return i;
    }

    @Override
    public int del(int id) {
        int i=0;
        String sql= "delete from rainquality where id=?";
        try {
            i=executeUpdate(sql,id);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(conn,null,null);
        }
        return i;
    }
}

